<script>


import {delForumReq, detail, forumReq, saveOrUpdate, filter} from "@/api/forum.js";
import {formatTime} from '@/utils/formatTime.js'

export default {
  name: "forum",
  data() {
    return {
      forumData: [],
      addDialogVisible: false,
      editDialogVisible: false,
      filterDialogVisible: false,
      addFormData: {},
      editFormData: {},
      filterFormData: {},
    }

  },

  methods: {
    formatTime,
    add() {
      saveOrUpdate(this.addFormData).then(res => {
        if (res.code === 200) {
          this.$message.success("添加成功")
          this.refresh()
          this.addDialogVisible = false
        } else {
          this.$message.error("添加失败")
        }
      })
    },
    edit() {
      saveOrUpdate(this.editFormData).then(res => {
        if (res.code === 200) {
          this.$message.success("修改成功")
          this.editDialogVisible = false
          this.refresh()
        } else {
          this.$message.error("修改失败")
        }
      })
    },
    handleEdit(row) {
      const data = {
        id: row.id
      }
      detail(data).then(res => {
        if (res.code === 200)
          this.editFormData = res.data
      })
      this.editDialogVisible = true;
    },
    handleDelete(id) {
      delForumReq(id).then(res => {
        if (res.code === 200) {
          this.$message.success("删除成功")
          this.refresh()
        } else {
          this.$message.error("删除失败")
        }
      })
    },
    refresh() {
      forumReq().then(res => {
        if (res.code === 200) {
          this.forumData = res.data
        } else {
          this.$message.error("获取数据失败")
        }
      })
    },
    openFormDialog() {
      this.addDialogVisible = true;
    },
    filters(data) {
      filter(data).then(res => {
        if (res.code === 200) {
          this.forumData = res.data
          this.filterDialogVisible = false
        } else {
          this.$message.error("获取数据失败")
        }
      })
    }

  },
  created() {
    this.refresh()
  }

}
</script>

<template>
  <div id="forum-title">
    论坛管理
  </div>
  <div id="forum-box">
    <div id="forum-form">
      <el-button type="primary" @click="filterDialogVisible = true">增加检索</el-button>
      <el-button type="primary" @click="openFormDialog">新增数据</el-button>
      <el-button type="primary">导入数据</el-button>
      <el-button type="primary">导出数据</el-button>
      <el-button type="primary" @click="refresh">刷新</el-button>
    </div>

    <div>
      <el-table id="forum-data" :data="forumData">
        <el-table-column type="selection" width="50px"/>
        <el-table-column label="类型" prop="type" width="100"></el-table-column>
        <el-table-column label="问题内容" prop="content" width="200"></el-table-column>
        <el-table-column label="图片" prop="img" width="200">
          <template #default="scope">
            <img :src="scope.row.imgs" style="width: 100px; height: 100px;" alt="图片">
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="createTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" prop="updateTime" width="200">
          <template #default="scope">
            <span>{{ formatTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button @click="handleEdit(scope.row)" type="primary">编辑</el-button>
            <el-button @click="handleDelete(scope.row.id)" type="danger">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

  </div>
  <!-- 添加表单 -->
  <div>
    <el-dialog :model-value="addDialogVisible" title="输入表单" :close-on-click-modal='false'>
      <el-form :model="addFormData" label-width="100px">
        <el-form-item label="类型">
          <el-input v-model="addFormData.type"></el-input>
        </el-form-item>
        <el-form-item label="问题内容">
          <el-input v-model="addFormData.content"></el-input>
        </el-form-item>
        <el-form-item label="图片">
          <el-input v-model="addFormData.img"></el-input>
        </el-form-item>
        <!-- 其他表单项 -->
        <el-form-item>
          <el-button type="primary" @click="add">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <!-- 编辑表单 -->
  <div>
    <el-dialog :model-value="editDialogVisible" title="编辑表单" :close-on-click-modal='false'>
      <el-form :model="editFormData" label-width="100px">
        <el-form-item label="类型">
          <el-input v-model="editFormData.type"></el-input>
        </el-form-item>
        <el-form-item label="问题内容">
          <el-input v-model="editFormData.content"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="info" @click="editDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="edit">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
  <!-- 过滤表单 -->
  <div>
    <el-dialog :model-value="filterDialogVisible" title="过滤表单" :close-on-click-modal='false'>
      <el-form>
        <el-form-item label="问题类型">
          <el-select v-model="filterFormData.type" placeholder="请选择问题类型">
            <el-option label="全部" value=""></el-option>
            <el-option label="问题" value="问题"></el-option>
            <el-option label="问大家" value="问大家"></el-option>
            <el-option label="差评" value="差评"></el-option>
            <el-option label="建议" value="建议"></el-option>
            <el-option label="其他" value="其他"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form-item>
        <el-button type="info" @click="filterDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="filters(filterFormData)">搜索</el-button>
      </el-form-item>
    </el-dialog>
  </div>
</template>

<style scoped>
#forum-box {
  background-color: #68a273;
  width: 90%;
  height: 90%;
  margin-top: 2%;
  margin-left: 3%;
  border-radius: 30px;

}

#forum-title {
  font-size: 30px;
  margin-left: 20px;
}

#forum-form {
  display: flex;
  padding-top: 40px;
  justify-content: flex-end;
  margin-right: 80px;
}


</style>